package h0721;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class SecretWord {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] words = sc.nextLine().split(" ");
        int[] arr = Arrays.stream(words).mapToInt(Integer::parseInt).toArray();
        int time = maxTime(arr);
        System.out.println(time);
    }
    private static int maxTime(int[] arr) {
        if (arr.length == 0) {
            return 0;
        }
        int ans = 0;
        LinkedList<Integer> stack = new LinkedList<>(); //来存放索引
        stack.addLast(0);
        while (stack.size() != 0) {
            int fa = stack.removeFirst();  //队列
            int ch1 = 2 * fa + 1;
            int ch2 = 2 * fa + 2;

            //检查fa是否存在左子节点
            boolean ch1_exist = ch1 < arr.length && arr[ch1] != -1;
            boolean ch2_exist = ch2 < arr.length && arr[ch2] != -1;

            if (ch1_exist) {
                arr[ch1] += arr[fa]; //累加
                stack.addLast(ch1);
            }
            if (ch2_exist) {
                arr[ch2] += arr[fa];
                stack.addLast(ch2);
            }
            //如果是叶子节点
            if (!ch1_exist && !ch2_exist) {
                ans = Math.max(ans, arr[fa]);
            }
        }
        return ans;
    }
}
